const cheerio = require('cheerio');
const fs = require('fs-extra');

const utils  = require('../lib/utils');

const getUrl = function(num) {
    return `http://zhao.resgain.net/name_list_${num}.html`;
}

let $ = "";
let lastNameArr = [];
const getLastNameArr = function(aArr) {
    let arr = [];
    aArr.each((i, elem) => {
        let lastName = $(elem).text();
        arr.push(lastName.substring(1, lastName.length));
    });
    return arr;
}

const main = async function () {
    const len = 10;
    for (let index = 1; index < len + 1; index++) {
        console.log(index);
        let reqResult = await utils.uget(getUrl(index));
        $ = cheerio.load(reqResult.body);
        let lastNames = getLastNameArr($(".container .row .col-xs-12").eq(2).find("a"))
        lastNameArr = lastNameArr.concat(lastNames);
    }
    fs.writeFileSync('./china_lastname.json', JSON.stringify(lastNameArr, null, 4));
}

main();

//const $ = cheerio.load('<h2 class="title">Hello world</h2>')